﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;
using System.Windows.Media;
using OxyPlot;

namespace Common.Utility
{
   public class ColorToBrushConverter : IValueConverter
   {
      #region IValueConverter Members

      public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
      {
         if (value == null) 
            return Brushes.Black; // Default color

         Color color = (Color)value;

         if (parameter != null && (parameter is bool))
         {
            if ((bool)parameter) //If parameter is true, use this just to pick either white or black
            {
               return Brushes.White;
            }
         }

         return new SolidColorBrush(color);
      }

      public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
      {
         if (value == null) 
            return Colors.Black; // Default color

         SolidColorBrush brush = (SolidColorBrush)value;

         return brush.Color;
      }

      #endregion
   }

   public class ComplimentaryBrushConverter : IValueConverter
   {
      #region IValueConverter Members

      public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
      {
         if (value == null)
            return Brushes.Black; // Default color

         Color color = (Color)value;

         OxyColor oxyColor = OxyColor.FromArgb(color.A, color.R, color.G, color.B);

         if (OxyColor.ColorDifference(oxyColor, OxyColor.FromUInt32(0)) < 1.0)
         {
            return Brushes.White;
         }
         else
         {
            return Brushes.Black;
         }
      }

      public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
      {
         if (value == null)
            return Colors.Black; // Default color

         SolidColorBrush brush = (SolidColorBrush)value;

         return brush.Color;
      }

      #endregion
   }
}
